import {
  get,
  post,
  put,
  del,
} from "./base_service"

// 商城相关接口=================
/**
  商城列表
 */
const queryStoreList = (page, pageSize) => {
  return new Promise((resolve, reject) => {
    get("/mall/list?page=" + page + "&pageSize=" + pageSize).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}
/**
 * 查询商品详情
 * @param {*} id 
 */
const queryStoreDetail = (id) => {
  return new Promise((resolve, reject) => {
    get("/mall/detail?id=" + id).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}
/**
 * 查询基地详情
 * @param {*} id 
 */
const queryBaseDetail = (id) => {
  return new Promise((resolve, reject) => {
    get("/health/base/detail?id=" + id).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 查询活动详情
 * @param {*} id 
 */
const queryActivityDetail = (id) => {
  return new Promise((resolve, reject) => {
    get("/activity/detail?id=" + id).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

// 商城相关接口=================

// 积分相关接口=================

/**
 * 绑定房源列表
 * @param {*} data 
 */
const bindingHouseList = () => {
  return new Promise((resolve, reject) => {
    get("/user/bind/house/list").then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 积分转增操作
 * @param {*} data 
 */
const sendScoreAction = (data) => {
  return new Promise((resolve, reject) => {
    post("/user/give/away/integral", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 抵扣操作
 * @param {*} data 
 */
const voucherHouseAction = (data) => {
  return new Promise((resolve, reject) => {
    post("/user/deduct/house/money", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}



// 积分相关接口=================

// 收获地址相关api========
/**
 * 创建收货地址
 * @param {*} data 
 */
const createReceiveAddress = (data) => {
  return new Promise((resolve, reject) => {
    post("/user/address/create", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}
/**
 * 获取收货地址列表
 */
const queryReceiveAddressList = () => {
  return new Promise((resolve, reject) => {
    get("/user/address/list").then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}
/**
 * 设置默认收货地址
 */
const setReceiveAddress = (id) => {
  return new Promise((resolve, reject) => {
    get("/user/address/set/default?id=" + id).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 更新收货地址
 * @param {*} data 
 */
const updateReceiveAddress = (data) => {
  return new Promise((resolve, reject) => {
    post("/user/address/update", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 删除收货地址
 * @param {*} id 
 */
const deleteReceiveAddress = (id) => {
  return new Promise((resolve, reject) => {
    del("/user/address/del?id=" + id).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}
/**
 * 获取单个地址详情
 * @param {*} id 
 */
const getReceiveAddressDetail = (id) => {
  return new Promise((resolve, reject) => {
    get("/user/address/detail?id=" + id).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

// 获取设置的默认收货地址
/**
 * 获取自己默认的收货地址
 */
const getAddressDefault = (id) => {
  return new Promise((resolve, reject) => {
    get("/user/address/default").then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


// 收获地址相关api========

// 个人中心相关api========
/**
 * 获取钱包
 */
const getMyWallet = () => {
  return new Promise((resolve, reject) => {
    get("/user/query/wallet").then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}
/**
 * 获取我的团队
 */
const getMyTeamList = (page, pageSize) => {
  return new Promise((resolve, reject) => {
    get("/user/next/Team?page=" + page + "&pageSize=" + pageSize).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 我的抵扣列表
 */
const getHouseApplyList = () => {
  return new Promise((resolve, reject) => {
    get("/user/deduct/apply/list").then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 我的订单列表
 */
const getMyOrderList = (page, pageSize, status) => {
  return new Promise((resolve, reject) => {
    get("/mall/order/list?page=" + page + "&pageSize=" + pageSize + "&status=" + status).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 活动列表
 */
const getActivityList = () => {
  return new Promise((resolve, reject) => {
    get("/activity/list").then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}
/**
 * 我报名的活动
 */
const signUpActivityList = () => {
  return new Promise((resolve, reject) => {
    get("/activity/my/sign/up").then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 活动报名
 */
const activitySignUpRequest = (data) => {
  return new Promise((resolve, reject) => {
    post("/activity/sign/up", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}
/**
 * 商品购买
 */
const shopBuyRequest = (data) => {
  return new Promise((resolve, reject) => {
    post("/activity/sign/up", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


/**
 * 绑定房源
 */
const bindHouseRequest = (data) => {
  return new Promise((resolve, reject) => {
    post("/user/bind/house", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


// 个人中心相关api========
/**
 * 首页基地城市列表切换
 */
const homeHealthBaseList = () => {
  return new Promise((resolve, reject) => {
    get("/health/base/list").then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


/**
 * 健康档案列表
 */
const getMyHealthList = () => {
  return new Promise((resolve, reject) => {
    get("/user/health/check?page=" + 0 + "&pageSize=" + 300).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 提现操作
 * @param {*} money 
 */
const scoreWithDrayRequest = (money) => {
  return new Promise((resolve, reject) => {
    get("/user/cash/money?money=" + money).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 购买活动商品
 */
const mallBuyRequest = (data) => {
  return new Promise((resolve, reject) => {
    post("/mall/buy", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 动态详情
 */
const queryDynDetail = (dynId) => {
  return new Promise((resolve, reject) => {
    get("/dyn/detail?dynId=" + dynId).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}
/**
 * 动态点赞
 */
const approveDynRequest = (dynId) => {
  return new Promise((resolve, reject) => {
    get("/dyn/approve?dynId=" + dynId).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 再次付款
 * @param {*} orderId 
 */
const payAgainAction = (orderId) => {
  return new Promise((resolve, reject) => {
    get("/mall/pay?orderId=" + orderId).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


/**
 * 删除订单
 * @param {*} orderId 
 */
const cancelOneOrder = (orderId) => {
  return new Promise((resolve, reject) => {
    get("/mall/del/order?orderId=" + orderId).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}
/**
 * 查询健康档案详情
 * @param {*} orderId 
 */
const queryHealthDetail = (id) => {
  return new Promise((resolve, reject) => {
    get("/user/health/check/detail?id=" + id).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}
/**
 * 确认收货
 * @param {*} orderId 
 */
const sureReciveProduct = (orderId) => {
  return new Promise((resolve, reject) => {
    get("/mall/ensure/goods?orderId=" + orderId).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}
/**
 * 申请退款
 * @param {*} orderId 
 */
const applyQuitProduct = (orderId) => {
  return new Promise((resolve, reject) => {
    get("/mall/apply/refunds?orderId=" + orderId).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 阅读数标记
 * @param {*} id 
 */
const viewNumberAction = (id) => {
  return new Promise((resolve, reject) => {
    get("/news/click?id=" + id).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 查询个人二维码
 */
const queryMyQrCode = () => {
  return new Promise((resolve, reject) => {
    get('/user/qr/code').then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}

// 购物车相关接口:

/**
 * 添加到购物车
 */
const mallAddCartRequest = (data) => {
  return new Promise((resolve, reject) => {
    post("/mall/add/cart", data).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}
/**
 * 查询购物车列表
 */
const queryCartList = (pageIndex = 0, pageSize = 1000) => {
  return new Promise((resolve, reject) => {
    get('/mall/cart/list?pageIndex=' + pageIndex + "&pageSize=" + pageSize).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}

/**
 * 购物车加数量
 */
const addCartNumRequest = (id, num) => {
  return new Promise((resolve, reject) => {
    get('/mall/add/cart/num?id=' + id + "&num=" + num).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}
/**
 * 删除一个购物车
 */
const delCartRequest = (id) => {
  return new Promise((resolve, reject) => {
    del('/mall/del/cart/' + id).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}



/**
 * 获取首页8个富文本
 * @param {*} id 
 */
const newHomeMenu = () => {
  return new Promise((resolve, reject) => {
    get('/home/menu').then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}
const queryRoleConfig = () => {
  return new Promise((resolve, reject) => {
    get('/user/get/role').then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}

/**
 * 获取富文本详情
 * @param {*} id 
 */
const queryHomeRichDetail = (id,isBottom = false) => {
  return new Promise((resolve, reject) => {
    let url = '/home/menu/'+id
    if(isBottom){
      url = '/home/rich/' + id
    }
    get(url).then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      wx.showToast({
        icon: "none",
        title: '网络异常稍后再试',
      })
    })
  })
}






module.exports = {
  queryStoreList,
  sendScoreAction,
  voucherHouseAction,
  createReceiveAddress,
  queryReceiveAddressList,
  setReceiveAddress,
  updateReceiveAddress,
  deleteReceiveAddress,
  getMyWallet,
  getReceiveAddressDetail,
  getMyTeamList,
  getMyOrderList,
  getActivityList,
  queryStoreDetail,
  bindHouseRequest,
  bindingHouseList,
  homeHealthBaseList,
  queryActivityDetail,
  getAddressDefault,
  activitySignUpRequest,
  shopBuyRequest,
  getMyHealthList,
  scoreWithDrayRequest,
  queryBaseDetail,
  mallBuyRequest,
  queryDynDetail,
  approveDynRequest,
  signUpActivityList,
  payAgainAction,
  cancelOneOrder,
  queryHealthDetail,
  sureReciveProduct,
  applyQuitProduct,
  getHouseApplyList,
  viewNumberAction,
  queryMyQrCode,
  mallAddCartRequest,
  queryCartList,
  addCartNumRequest,
  delCartRequest,
  newHomeMenu,
  queryHomeRichDetail,
  queryRoleConfig,
}

